//
//  DisplayObject.mm
//
//  Created by Colin Leung on 2010-05-21.
//  Copyright (c) 2010 digi3studio. All rights reserved.
//

#import "DisplayObject.h"

@implementation DisplayObject

@synthesize layer;

@synthesize accessibilityProperties;
@synthesize alpha;
@synthesize blendMode;
//@synthesize blendShader;
//@synthesize cacheAsBitmap;
//@synthesize filters;
@synthesize height;
@synthesize loaderInfo;
@synthesize mask;
@synthesize mouseX;
@synthesize mouseY;
@synthesize name;
@synthesize opaqueBackground;
@synthesize parent;
@synthesize root;
@synthesize rotation;
@synthesize rotationX;
@synthesize rotationY;
@synthesize rotationZ;
@synthesize scale9Grid;
@synthesize scaleX;
@synthesize scaleY;
@synthesize scaleZ;
@synthesize scrollRect;
@synthesize stage;
@synthesize transform;
@synthesize visible;
@synthesize width;
@synthesize x;
@synthesize y;
@synthesize z;

- (id)init{
	if((self = [super init])){
		eventBase = [[EventDispatcher alloc] init];
		layer = [CALayer layer];
		stage = nil;
		parent= nil;
	}
	return self;
}

-(void) setX:(float)ax{
	x = ax;
	layer.position = CGPointMake(x,y);
}

-(void) setY:(float)ay{
	y = ay;
	layer.position = CGPointMake(x,y);
}

-(CGRect) getBounds:(DisplayObject*)_targetCoordinateSpace{
	return [layer convertRect: layer.frame toLayer:_targetCoordinateSpace.layer];
}

-(CGRect) getRect:(DisplayObject*)_targetCoordinateSpace{
	return [layer convertRect: layer.frame toLayer:_targetCoordinateSpace.layer];
}

-(CGPoint) globalToLocal:(CGPoint)_point{
	if(stage==nil)return CGPointMake(0,0);
	return [layer convertPoint:_point fromLayer:stage.layer];
}

//-(Vector3D*) globalToLocal3D:(CGPoint)_point{}

-(BOOL) hitTestObject:(DisplayObject*)obj{
	return CGRectIntersectsRect(layer.frame, obj.layer.frame);
}

-(BOOL) hitTestPoint:(double)_x y:(double)_y{
	return [self hitTestPoint:_x y:_y shapeFlag:NO];
}

-(BOOL) hitTestPoint:(double)_x y:(double)_y shapeFlag:(BOOL)_shapeFlag{
	return ([layer hitTest:CGPointMake(_x,_y)]!=nil);
}

//-(CGPoint) local3DToGlobal:(Vector3D*)_point3d{}

-(CGPoint) localToGlobal:(CGPoint)_point{
	if(stage==nil)return CGPointMake(0,0);
	return  [layer convertPoint:_point toLayer:stage.layer];
}

-(void)addEventListener:(NSString*)_type listener:(ASFunction*)_listener{
	[eventBase addEventListener:_type listener:_listener];
}

-(void)addEventListener:(NSString*)_type listener:(ASFunction*)_listener useCapture:(BOOL)_useCapture priority:(int)_priority useWeakReference:(BOOL)_useWeakReference{
	[eventBase addEventListener:_type listener:_listener useCapture:_useCapture priority:_priority useWeakReference:_useWeakReference];
}

-(BOOL)dispatchEvent:(Event*)event{
	return [eventBase dispatchEvent:event];
}

-(BOOL)hasEventListener:(NSString*)_type{
	return [eventBase hasEventListener:_type];
}

-(void)removeEventListener:(NSString*)_type listener:(ASFunction*)_listener{
	[eventBase removeEventListener:_type listener:_listener];
}

-(void)removeEventListener:(NSString*)_type listener:(ASFunction*)_listener useCapture:(BOOL)_useCapture{
	[eventBase removeEventListener:_type listener:_listener useCapture:_useCapture];
}

-(BOOL)willTrigger:(NSString*)_type{
	return [eventBase willTrigger:_type];
}

-(void) dealloc{
	[eventBase release];
	[super dealloc];
}

@end